package com.lgmshare.k3.ui.dialog;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lgmshare.k3.R;
import com.lgmshare.component.app.LaraDialog;


/**
 * 通用自定义样式
 *
 * @author: lim
 * @ClassName: SimpleDialog.java
 * @datetime 2014-11-11 上午9:38:40
 */
public class CommonDialog extends LaraDialog {

    private View dialog_line;
    private TextView dialog_tv_title;
    private TextView dialog_tv_message;
    private Button dialog_btn_positive;
    private Button dialog_btn_negative;

    private CharSequence mTitle;
    private CharSequence mContent;
    private CharSequence mPositive;
    private CharSequence mNegative;
    private View.OnClickListener mPositiveClickListener;
    private View.OnClickListener mNegativeClickListener;

    public CommonDialog(Context context) {
        super(context, R.style.CommonDialog);
    }

    public CommonDialog(Context context, CharSequence title, CharSequence content) {
        super(context, R.style.CommonDialog);
        this.mTitle = title;
        this.mContent = content;
    }

    public CommonDialog(Context context, int theme, CharSequence title, CharSequence content) {
        super(context, theme);
        this.mTitle = title;
        this.mContent = content;
    }

    @Override
    protected int onBindLayoutResId() {
        return R.layout.dialog_commom;
    }

    @Override
    protected void initView() {
        setCancelable(true);
        setCanceledOnTouchOutside(false);
        dialog_tv_title = findViewById(R.id.dialog_tv_title);
        dialog_tv_message = findViewById(R.id.dialog_tv_message);
        dialog_btn_negative = findViewById(R.id.dialog_btn_negative);
        dialog_line = findViewById(R.id.dialog_line);
        dialog_btn_positive = findViewById(R.id.dialog_btn_positive);

        //######################标题######################
        if (!TextUtils.isEmpty(mTitle)) {
            dialog_tv_title.setText(mTitle);
            dialog_tv_title.setVisibility(View.VISIBLE);
        } else {
            dialog_tv_title.setVisibility(View.GONE);
        }

        //######################内容######################
        dialog_tv_message.setText(mContent);

        //######################按钮######################
        //右按钮(确定)
        if (mPositiveClickListener != null) {
            dialog_btn_positive.setText(mPositive);
            dialog_btn_positive.setOnClickListener(mPositiveClickListener);
        } else {
            dialog_btn_positive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }

        //左按钮（取消）
        if (mNegativeClickListener != null) {
            dialog_btn_negative.setText(mNegative);
            dialog_btn_negative.setVisibility(View.VISIBLE);
            dialog_btn_negative.setOnClickListener(mNegativeClickListener);
        } else {
            dialog_line.setVisibility(View.GONE);
            dialog_btn_negative.setVisibility(View.GONE);
            dialog_btn_positive.setBackgroundResource(R.drawable.dialog_btn_single_bg);
        }
    }

    @Override
    public void setTitle(int resId) {
        String title = getContext().getResources().getString(resId);
        setTitle(title);
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
    }

    public void setContent(int resId) {
        String content = getContext().getResources().getString(resId);
        setContent(content);
    }

    public void setContent(CharSequence content) {
        mContent = content;
    }

    /**
     * 设置按钮（否定）
     *
     * @param resId
     * @param onClickListener
     */
    public void setOnNegativeClick(int resId, final View.OnClickListener onClickListener) {
        mNegative = getContext().getResources().getString(resId);
        mNegativeClickListener = onClickListener;
    }

    /**
     * 设置按钮（否定）
     *
     * @param name
     * @param onClickListener
     */
    public void setOnNegativeClick(CharSequence name, final View.OnClickListener onClickListener) {
        mNegative = name;
        mNegativeClickListener = onClickListener;
    }

    /**
     * 设置按钮（确定）
     *
     * @param resId
     * @param onClickListener
     */
    public void setOnPositiveClick(int resId, final View.OnClickListener onClickListener) {
        mPositive = getContext().getResources().getString(resId);
        mPositiveClickListener = onClickListener;
    }

    /**
     * 设置按钮（确定）
     *
     * @param name
     * @param onClickListener
     */
    public void setOnPositiveClick(CharSequence name, final View.OnClickListener onClickListener) {
        mPositive = name;
        mPositiveClickListener = onClickListener;
    }

    public static class Builder {

        private Context mContext;
        private int mThemeId;
        private CharSequence mTitle;
        private CharSequence mMessage;
        private CharSequence mPositive;
        private CharSequence mNegative;
        private View.OnClickListener mPositiveClickListener;
        private View.OnClickListener mNegativeClickListener;
        private OnCancelListener mOnCancelListener;
        private OnDismissListener mOnDismissListener;
        private OnKeyListener mOnKeyListener;
        private boolean mCancelable;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder(Context context, int themeResId) {
            mContext = context;
            mThemeId = themeResId;
        }

        public Context getContext() {
            return mContext;
        }

        public CommonDialog.Builder setTitle(int titleId) {
            mTitle = getContext().getResources().getString(titleId);
            return this;
        }

        public CommonDialog.Builder setTitle(CharSequence title) {
            mTitle = title;
            return this;
        }

        public CommonDialog.Builder setMessage(int messageId) {
            mMessage = getContext().getResources().getString(messageId);
            return this;
        }

        public CommonDialog.Builder setMessage(CharSequence message) {
            mMessage = message;
            return this;
        }

        public CommonDialog.Builder setIcon(int iconId) {
            return this;
        }

        public CommonDialog.Builder setIcon(Drawable icon) {
            return this;
        }

        public CommonDialog.Builder setPositiveButton(int textId, View.OnClickListener listener) {
            mPositive = getContext().getResources().getString(textId);
            mPositiveClickListener = listener;
            return this;
        }

        public CommonDialog.Builder setPositiveButton(CharSequence text, View.OnClickListener listener) {
            mPositive = text;
            mPositiveClickListener = listener;
            return this;
        }

        public CommonDialog.Builder setNegativeButton(int textId, View.OnClickListener listener) {
            mNegative = getContext().getResources().getString(textId);
            mNegativeClickListener = listener;
            return this;
        }

        public CommonDialog.Builder setNegativeButton(CharSequence text, View.OnClickListener listener) {
            mNegative = text;
            mNegativeClickListener = listener;
            return this;
        }

        public CommonDialog.Builder setCancelable(boolean cancelable) {
            mCancelable = cancelable;
            return this;
        }

        public CommonDialog.Builder setOnCancelListener(OnCancelListener onCancelListener) {
            mOnCancelListener = onCancelListener;
            return this;
        }

        public CommonDialog.Builder setOnDismissListener(OnDismissListener onDismissListener) {
            mOnDismissListener = onDismissListener;
            return this;
        }

        public CommonDialog.Builder setOnKeyListener(OnKeyListener onKeyListener) {
            mOnKeyListener = onKeyListener;
            return this;
        }

        public CommonDialog create() {
            // Context has already been wrapped with the appropriate theme.
            final CommonDialog dialog = new CommonDialog(mContext, mThemeId, mTitle, mMessage);
            dialog.setCancelable(mCancelable);
            dialog.setOnNegativeClick(mNegative, mNegativeClickListener);
            dialog.setOnPositiveClick(mPositive, mPositiveClickListener);
            if (mCancelable) {
                dialog.setCanceledOnTouchOutside(true);
            }
            if (mOnCancelListener != null) {
                dialog.setOnCancelListener(mOnCancelListener);
            }
            if (mOnDismissListener != null) {
                dialog.setOnDismissListener(mOnDismissListener);
            }
            if (mOnKeyListener != null) {
                dialog.setOnKeyListener(mOnKeyListener);
            }
            return dialog;
        }

        public CommonDialog show() {
            final CommonDialog dialog = create();
            dialog.show();
            return dialog;
        }
    }
}
